package gtime

import (
	"strings"
	"time"
)

func StrToTime(datetime string) int64 {
	var t string
	if find := strings.Contains(datetime, "-"); find && len(datetime) == 10 {
		t = "2006-01-02"
	}
	if find := strings.Contains(datetime, "/"); find && len(datetime) == 10 {
		t = "2006/01/02"
	}
	if find := strings.Contains(datetime, "-"); find && len(datetime) == 19 {
		t = "2006-01-02 15:04:05"
	}
	if find := strings.Contains(datetime, "/"); find && len(datetime) == 19 {
		t = "2006/01/02 15:04:05"
	}
	stamp, err := time.ParseInLocation(t, datetime, time.Local)
	if err != nil || t == "" {
		return 0
	}
	return stamp.Unix()
}

/*
 * 日期字符串
 */
func Date(format int) string {
	switch format {
	case 1:
		return time.Now().Local().Format("2006-01-02")
	case 2:
		return time.Now().Local().Format("2006/01/02")
	case 3:
		return time.Now().Local().Format("2006-01-02 15:04:05")
	case 4:
		return time.Now().Local().Format("2006/01/02 15:04:05")
	case 5:
		return time.Now().Local().Format("01-02")
	case 6:
		return time.Now().Local().Format("01/02")
	case 7:
		return time.Now().Local().Format("01-02 15:04:05")
	case 8:
		return time.Now().Local().Format("01/02 15:04:05")
	case 9:
		return time.Now().Local().Format("20060102")
	case 10:
		return time.Now().Local().Format("20060102150405")
	default:
		return time.Now().Local().Format("2006-01-02 15:04:05")
	}
}

/*
 * 当前时间戳
 */
func TimeStamp() int64 {
	return time.Now().Unix()
}

/*
 * 当日时间戳开始和结束
 */
func TodayStamp() (int64, int64) {
	t := time.Now()
	start := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()).Unix()
	end := time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 0, t.Location()).Unix()
	return start, end
}

/*
 * 昨日时间戳开始和结束
 */
func YesterdayStamp() (int64, int64) {
	NowTime := time.Now()
	var startTime time.Time
	if NowTime.Hour() == 0 && NowTime.Minute() == 0 && NowTime.Second() == 0 {
		startTime = time.Unix(NowTime.Unix()-86399, 0) //当天的最后一秒
	} else {
		startTime = time.Unix(NowTime.Unix()-86400, 0)
	}
	currentYear := startTime.Year()
	currentMonth := startTime.Month()
	currentDay := startTime.Day()
	yesterdayStartTime := time.Date(currentYear, currentMonth, currentDay, 0, 0, 0, 0, time.Local).Unix()
	yesterdayEndTime := time.Date(currentYear, currentMonth, currentDay, 23, 59, 59, 0, time.Local).Unix()
	return yesterdayStartTime, yesterdayEndTime
}
